#!/usr/bin/env python
# -*- coding: UTF-8 -*-

class VersionException(Exception):
    def __init__(self, message):
        self.__message = message

    def __str__(self):
        return self.__message

class Version:
    class Any:
        def __cmp__(self, other):
            return 0
            
        def __repr__(self):
            return "*"
            
        def __str__(self):
            return  "*"

    def __init__(self, version_string):
        self.__any = Version.Any()
        self.__major, self.__minor, self.__build, self.__revision = self.__get_version(version_string)
        
    def __repr__(self):
        return "%s.%s.%s.%s" % (self.__major, self.__minor, self.__build, self.__revision)

    def __str__(self):
        return "%s.%s.%s.%s" % (self.__major, self.__minor, self.__build, self.__revision)
        
    @property
    def major(self):
        return self.__major

    @property
    def minor(self):
        return self.__minor

    @property
    def build(self):
        return self.__build

    @property
    def revision(self):
        return self.__revision

    def __get_version(self, version_string):        
        result = [self.__any, self.__any, self.__any, self.__any]
        
        version_parts = version_string.split(".")
        if version_parts != [""]:
            result[:len(version_parts)] = version_parts

        try:
            return map(self.__string_to_int, result)
        except:
            raise VersionException('Could not process: ' + version_string)

    def __string_to_int(self, part):
        if part == "":
            return self.__any
        else:
            return int(part)

    def __cmp__(self, other):
        if isinstance(other, Version.Any):
            return cmp(other, self)

        if other in [None, ()]:
            return 1

        if isinstance(other, MaxVersion):
            return -1

        if isinstance(other, MinVersion):
            return 1

        if self.__major == other.major:
            if self.__minor == other.minor:
                if self.__build == other.build:
                    return cmp(self.__revision, other.revision)
                else:
                    return cmp(self.__build, other.build)
            else:
                return cmp(self.__minor, other.minor)
        else:
            return cmp(self.__major, other.major)

class MaxVersion(Version):
    def __init__(self, version_string):
        if version_string != "":
            raise VersionException("Max version could not be specified: " + version_string)

    def __cmp__(self, other):
        return 1

class MinVersion(Version):
    def __init__(self, version_string):
        if version_string != "":
            raise VersionException("Min version could not be specified: " + version_string)

    def __cmp__(self, other):
        return -1